home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Magazine / GraphicsCards / StormMesa / demos / bounce.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-12-15  |  3.6 KB  |  212 lines

  1. /* $Id: bounce.c,v 3.0 1998/02/14 18:42:29 brianp Exp $ */
  2.  
  3. /*
  4.  * Bouncing ball demo.  Color index mode only!
  5.  *
  6.  * This program is in the public domain
  7.  *
  8.  * Brian Paul
  9.  */
  10.  
  11. /* Conversion to GLUT by Mark J. Kilgard */
  12.  
  13. /*
  14.  * $Log: bounce.c,v $
  15.  * Revision 3.0  1998/02/14 18:42:29  brianp
  16.  * initial rev
  17.  *
  18.  */
  19.  
  20.  
  21. #include <math.h>
  22. #include <stdlib.h>
  23. #include <GL/glut.h>
  24.  
  25. #define COS(X)   cos( (X) * 3.14159/180.0 )
  26. #define SIN(X)   sin( (X) * 3.14159/180.0 )
  27.  
  28. #define RED 1
  29. #define WHITE 2
  30. #define CYAN 3
  31.  
  32. GLuint Ball;
  33. GLenum Mode;
  34. GLfloat Zrot = 0.0, Zstep = 6.0;
  35. GLfloat Xpos = 0.0, Ypos = 1.0;
  36. GLfloat Xvel = 0.2, Yvel = 0.0;
  37. GLfloat Xmin = -4.0, Xmax = 4.0;
  38. GLfloat Ymin = -3.8, Ymax = 4.0;
  39. GLfloat G = -0.1;
  40.  
  41. static GLuint 
  42. make_ball(void)
  43. {
  44.   GLuint list;
  45.   GLfloat a, b;
  46.   GLfloat da = 18.0, db = 18.0;
  47.   GLfloat radius = 1.0;
  48.   GLuint color;
  49.   GLfloat x, y, z;
  50.  
  51.   list = glGenLists(1);
  52.  
  53.   glNewList(list, GL_COMPILE);
  54.  
  55.   color = 0;
  56.   for (a = -90.0; a + da <= 90.0; a += da) {
  57.  
  58.     glBegin(GL_QUAD_STRIP);
  59.     for (b = 0.0; b <= 360.0; b += db) {
  60.  
  61.       if (color) {
  62.         glIndexi(RED);
  63.       } else {
  64.         glIndexi(WHITE);
  65.       }
  66.  
  67.       x = COS(b) * COS(a);
  68.       y = SIN(b) * COS(a);
  69.       z = SIN(a);
  70.       glVertex3f(x, y, z);
  71.  
  72.       x = radius * COS(b) * COS(a + da);
  73.       y = radius * SIN(b) * COS(a + da);
  74.       z = radius * SIN(a + da);
  75.       glVertex3f(x, y, z);
  76.  
  77.       color = 1 - color;
  78.     }
  79.     glEnd();
  80.  
  81.   }
  82.  
  83.   glEndList();
  84.  
  85.   return list;
  86. }
  87.  
  88. static void 
  89. reshape(int width, int height)
  90. {
  91.   glViewport(0, 0, (GLint) width, (GLint) height);
  92.   glMatrixMode(GL_PROJECTION);
  93.   glLoadIdentity();
  94.   glOrtho(-6.0, 6.0, -6.0, 6.0, -6.0, 6.0);
  95.   glMatrixMode(GL_MODELVIEW);
  96. }
  97.  
  98. /* ARGSUSED1 */
  99. static void
  100. key(unsigned char k, int x, int y)
  101. {
  102.   switch (k) {
  103.   case 27:  /* Escape */
  104.     exit(0);
  105.   }
  106. }
  107.  
  108. static void 
  109. draw(void)
  110. {
  111.   GLint i;
  112.  
  113.   glClear(GL_COLOR_BUFFER_BIT);
  114.  
  115.   glIndexi(CYAN);
  116.   glBegin(GL_LINES);
  117.   for (i = -5; i <= 5; i++) {
  118.     glVertex2i(i, -5);
  119.     glVertex2i(i, 5);
  120.   }
  121.   for (i = -5; i <= 5; i++) {
  122.     glVertex2i(-5, i);
  123.     glVertex2i(5, i);
  124.   }
  125.   for (i = -5; i <= 5; i++) {
  126.     glVertex2i(i, -5);
  127.     glVertex2f(i * 1.15, -5.9);
  128.   }
  129.   glVertex2f(-5.3, -5.35);
  130.   glVertex2f(5.3, -5.35);
  131.   glVertex2f(-5.75, -5.9);
  132.   glVertex2f(5.75, -5.9);
  133.   glEnd();
  134.  
  135.   glPushMatrix();
  136.   glTranslatef(Xpos, Ypos, 0.0);
  137.   glScalef(2.0, 2.0, 2.0);
  138.   glRotatef(8.0, 0.0, 0.0, 1.0);
  139.   glRotatef(90.0, 1.0, 0.0, 0.0);
  140.   glRotatef(Zrot, 0.0, 0.0, 1.0);
  141.  
  142.   glCallList(Ball);
  143.  
  144.   glPopMatrix();
  145.  
  146.   glFlush();
  147.   glutSwapBuffers();
  148. }
  149.  
  150. static void 
  151. idle(void)
  152. {
  153.   static float vel0 = -100.0;
  154.  
  155.   Zrot += Zstep;
  156.  
  157.   Xpos += Xvel;
  158.   if (Xpos >= Xmax) {
  159.     Xpos = Xmax;
  160.     Xvel = -Xvel;
  161.     Zstep = -Zstep;
  162.   }
  163.   if (Xpos <= Xmin) {
  164.     Xpos = Xmin;
  165.     Xvel = -Xvel;
  166.     Zstep = -Zstep;
  167.   }
  168.   Ypos += Yvel;
  169.   Yvel += G;
  170.   if (Ypos < Ymin) {
  171.     Ypos = Ymin;
  172.     if (vel0 == -100.0)
  173.       vel0 = fabs(Yvel);
  174.     Yvel = vel0;
  175.   }
  176.   glutPostRedisplay();
  177. }
  178.  
  179. void 
  180. visible(int vis)
  181. {
  182.   if (vis == GLUT_VISIBLE)
  183.     glutIdleFunc(idle);
  184.   else
  185.     glutIdleFunc(NULL);
  186. }
  187.  
  188. int main(int argc, char *argv[])
  189. {
  190.   glutInit(&argc, argv);
  191.   glutInitDisplayMode(GLUT_INDEX | GLUT_DOUBLE);
  192.  
  193.   glutCreateWindow("Bounce");
  194.   Ball = make_ball();
  195.   glCullFace(GL_BACK);
  196.   glEnable(GL_CULL_FACE);
  197.   glDisable(GL_DITHER);
  198.   glShadeModel(GL_FLAT);
  199.  
  200.   glutDisplayFunc(draw);
  201.   glutReshapeFunc(reshape);
  202.   glutVisibilityFunc(visible);
  203.   glutKeyboardFunc(key);
  204.  
  205.   glutSetColor(RED, 1.0, 0.0, 0.0);
  206.   glutSetColor(WHITE, 1.0, 1.0, 1.0);
  207.   glutSetColor(CYAN, 0.0, 1.0, 1.0);
  208.  
  209.   glutMainLoop();
  210.   return 0;             /* ANSI C requires main to return int. */
  211. }
  212.